package com.test06_字符串;

import org.junit.Test;

import java.util.Arrays;

/**
 * String类常用方法
 */
public class YootkDemo_02 {

    @Test
    public void test1() {
        String messageA = "www.yootk.com";
        String messageB = "www.YOOTK.com";
        System.out.println("【字符串长度】" + messageA.length());
        System.out.println("【获取指定位置字符】" + messageA.charAt(4));
        System.out.println("【判断内容是否一致】" + messageA.equals(messageB));
        System.out.println("【判断内容是否一致】" + messageA.equalsIgnoreCase(messageB));     // 不区分大小写
        System.out.println("【字符串大小比较】" + messageA.compareTo(messageB));
        System.out.println("【字符串大小比较】" + messageA.compareToIgnoreCase(messageB));   // 不区分大小写
        System.out.println("【是否包含子串】" + messageA.contains("yootk"));
        System.out.println("【查找索引位置】" + messageA.indexOf("yootk"));
        System.out.println("【判断开头】" + messageA.startsWith("www."));
        System.out.println("【判断结尾】" + messageA.endsWith(".com"));
        System.out.println("【字符串替换】" + messageA.replaceAll("yootk", "baidu"));
        System.out.println("【字符串拆分】" + Arrays.toString(messageA.split("\\.")));
        System.out.println("【字符串截取】" + messageA.substring(4));
        System.out.println("【字符串截取】" + messageA.substring(4, 9));
        System.out.println("【去掉两端空格】" + messageA.trim());
        System.out.println("【字符串转小写】" + messageA.toLowerCase());
        System.out.println("【字符串转大写】" + messageA.toUpperCase());
    }

    @Test
    public void test2() {
        {
            // 字符串转为字符数组
            String message = "www.yootk.com";
            char[] chars = message.toCharArray();
            // 字符数组转为字符串
            System.out.println(new String(chars));
            System.out.println(new String(chars, 4, 5));
        }
        {
            // 字符串转为字节数组
            String message = "www.yootk.com";
            byte[] bytes = message.getBytes();
            // 字节数组转为字符串
            System.out.println(new String(bytes));
            System.out.println(new String(bytes, 4, 5));
        }
    }

    @Test
    public void test3() {
        String name = "李小帅";
        int age = 18;
        double score = 91.5;
        String result = String.format("姓名：%s、年龄：%d、成绩：%5.2f", name, age, score);    // 格式化处理
        System.out.println(result);
    }

    @Test
    public void test4() {
        String message = "192.168.115.119";
        // "."在正则表达式里面有特殊含义，必须转移
        String[] result = message.split("\\.");
        for (String temp : result) {
            System.out.print(temp + "、");
        }
    }
}
